package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.DefendRegisterMapper;
import com.xidian.zhiancloud.dao.DefendTypeMapper;
import com.xidian.zhiancloud.dao.DeviceTypeMapper;
import com.xidian.zhiancloud.dao.EqFacMngMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.equipFacility.DefendRegisterDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.DefendLedgerQO;
import com.xidian.zhiancloud.qo.equipFacility.DefendRegisterQO;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.equipFacility.DefendLedgerVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendRegisterVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 维护登记
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@Service
public class DefendRegisterService extends ServiceImpl<DefendRegisterMapper, DefendRegister> implements IService<DefendRegister> {

    @Autowired
    private DefendRegisterMapper defendRegisterMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private DefendTypeMapper defendTypeMapper;

    @Autowired
    private EqFacMngMapper eqFacMngMapper;

    /**
     * 添加维护登记
     * @param defendRegisterDTO
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean add(DefendRegisterDTO defendRegisterDTO){

        DefendRegister defendRegister = defendRegisterDTO.getDefendRegister();
        List<SysUser> defenders = defendRegisterDTO.getDefenders();

        String ids = "";

        for (SysUser defender : defenders) {
            ids += defender.getId() + ",";
        }

        defendRegister.setDefenderIds(ids);
        defendRegister.setStatus(0);

        int insert = defendRegisterMapper.insert(defendRegister);
        if(insert == 0) {
            throw new CustomizeException(GeneralResponseEnums.ADD_FAILED);
        }
        return true;
    }

    /**
     * 更新维护登记信息
     * @param defendRegisterDTO
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean patchById(DefendRegisterDTO defendRegisterDTO){
        DefendRegister defendRegister = defendRegisterDTO.getDefendRegister();
        List<SysUser> defenders = defendRegisterDTO.getDefenders();

        String ids = "";

        for (SysUser defender : defenders) {
            ids += defender.getId() + ",";
        }

        defendRegister.setDefenderIds(ids);
        defendRegister.setStatus(0);

        int ret = defendRegisterMapper.updateById(defendRegister);
        if(ret == 0){
            throw new CustomizeException(GeneralResponseEnums.UPDATE_FAILED);
        }

        return true;
    }

    /**
     * 根据id删除维护登记记录
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean deleteByIds(Long[] ids){

        if(ids == null || ids.length == 0){
            return false;
        }

        for (Long id : ids) {
            int ret = defendRegisterMapper.deleteById(id);
            if(ret == 0){
                throw new CustomizeException(GeneralResponseEnums.DELETE_FAILED);
            }
        }

        return true;
    }

    /**
     * 列表查询
     * @param defendRegisterQO
     * @return
     */
    public IPage<DefendRegisterVO> getListByQO(DefendRegisterQO defendRegisterQO){

        Page<DefendRegisterVO> page = new Page<>(defendRegisterQO.getCurrent(), defendRegisterQO.getSize());

        IPage<DefendRegisterVO> list = defendRegisterMapper.selectByQO(page,defendRegisterQO);

        if(list != null){
            list.getRecords().forEach(vo -> {

                Organization organization = organizationMapper.selectById(vo.getMngCtrDeptId());
                vo.setDepartmentName(organization == null ? "该部门不存在" : organization.getName());

                DefendType defendType = defendTypeMapper.selectById(vo.getDefendTypeId());
                vo.setDefendTypeName(defendType == null ? "该类型不存在" : defendType.getName());

                DeviceType deviceType = deviceTypeMapper.selectById(vo.getDeviceTypeId());
                vo.setDeviceName(deviceType == null ? "该类型不存在" : deviceType.getName());

                String ids = vo.getDefenderIds();
                long[] array = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).toArray();
                String name = "";
                for (long id : array) {
                    SysUser sysUser = sysUserMapper.selectById(id);
                    if(sysUser != null){
                        name += sysUser.getName() + ",";
                    }
                }
                vo.setDefenderName(name);
            });
        }

        return list;
    }

    /**
     * 提交
     * @param id
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean submit(Long id){

        DefendRegister defendRegister = defendRegisterMapper.selectById(id);
        if(defendRegister == null){
            throw new CustomizeException(GeneralResponseEnums.SUBMIT_FAILED);
        }

        defendRegister.setStatus(1);

        int ret = defendRegisterMapper.updateById(defendRegister);
        if(ret == 0){
            throw new CustomizeException(GeneralResponseEnums.SUBMIT_FAILED);
        }

        return true;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public DefendRegisterVO getById(Long id){

        DefendRegister defendRegister = defendRegisterMapper.selectById(id);
        DefendRegisterVO vo = new DefendRegisterVO();
        BeanUtils.copyProperties(defendRegister,vo);

        EqFacMng eqFacMng = eqFacMngMapper.selectById(vo.getDeviceId());
        vo.setDeviceName(eqFacMng==null ? "该设备不存在" :eqFacMng.getName());

        Organization organization = organizationMapper.selectById(eqFacMng.getMngCtrDeptId());
        vo.setDepartmentName(organization == null ? "该部门不存在" : organization.getName());

        DeviceType deviceType = deviceTypeMapper.selectById(eqFacMng.getDeviceTypeId());
        vo.setDefendTypeName(deviceType == null ? "该类型不存在" : deviceType.getName());

        DefendType defendType = defendTypeMapper.selectById(vo.getDefendTypeId());
        vo.setDefendTypeName(defendType == null ? "该类型不存在" : defendType.getName());

        String ids = vo.getDefenderIds();
        long[] array = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).toArray();
        String name = "";
        for (long i : array) {
            SysUser sysUser = sysUserMapper.selectById(i);
            if(sysUser != null){
                name = sysUser.getName() + ",";
            }
        }

        vo.setDefenderName(name);

        return vo;
    }
    
}
